//获取应用实例
const app = getApp();

Page({
	data: {
    goods_id:'',
    is_collect: false,
  },
	onLoad: function(options) {
    // console.log(options.goods_id)
    this.setData({
      goods_id: options.goods_id
    })
    this.getCommentStatistics(options.goods_id);
  },

  //获取商品评论统计
  getCommentStatistics(goods_id) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Goods/getCommentStatistics',
      data:
      {
        goods_id: goods_id
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.code == 1) {
          that.setData({
            commentStatistics: res.data.data.statistics,
          })
          //判断是否有评论
          if (res.data.data.statistics.allnum > 0) {
            that.getComment(goods_id);
          }
          that.is_collect();
        }
      }, fail: function () {

      }
    });
  },
  //获取商品评论列表
  getComment(goods_id) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Goods/getComment',
      data:
      {
        goods_id: goods_id
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        // console.log(res);
        if (res.data.data.list.length > 0) {
          for (var i = 0; i < res.data.data.list.length; i++) {
            res.data.data.list[i].img = app.globalData.imgUrl + "" + res.data.data.list[i].img;
            res.data.data.list[i].user_avatar = app.globalData.imgUrl + "" + res.data.data.list[i].user_avatar;
            that.setData({
              commentList: res.data.data.list,
            })
          }
        }

      }, fail: function () {

      }
    });
  },

  //订单管理接口
  scanbuy() {
    this.submitOrder();
  },

  //立即购买
  submitOrder() {
    wx.navigateTo({
      url: 'order?goods_id=' + this.data.goods_id,
    })
  },


  //用户是否收藏商品
  is_collect: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/GoodsCollect/is_collect',
      data: {
        token: app.globalData.token,
        goods_id: that.data.goods_id,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        if (res.data.code == 1) {
          if (res.data.data.is_collect == 'Y') {
            that.setData({
              is_collect: true
            })
          }
        }
      }
    })
  },

  //收藏商品
  collect: function () {
    var that = this;
    if (that.data.is_collect == true) {
      that.cancel();
    } else {
      that.saveCollect();
    }

  },

  saveCollect: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/GoodsCollect/collect',
      data: {
        token: app.globalData.token,
        goods_id: that.data.goods_id,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        // console.log(res)
        if (res.data.code == 1) {
          that.setData({
            is_collect: true
          })
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

  //取消收藏
  cancel: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/GoodsCollect/cancel',
      data: {
        token: app.globalData.token,
        goods_id: that.data.goods_id,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        // console.log(res)
        if (res.data.code == 1) {
          that.setData({
            is_collect: false
          })
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

  //加入购物车
  addCart() {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Shopcart/add',
      data:
      {
        token: app.globalData.token,
        goods_id: that.data.goods_id,
        goods_num: 1,
        // goods_sku:''
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        // console.log(res);
        if (res.data.code == 1) {
          wx.showToast({
            title: '已加入购物车',
            icon: 'success'
          })
        }
      }, fail: function () {

      }
    });
  },


//购物车
  shopCart: function () {
    if (app.globalData.token != '') {
      wx.navigateTo({
        url: 'shop-cart',
      })
    } else {
      wx.reLaunch({
        url: 'login',
      })
    }

  },
});
